﻿﻿using System;
﻿using System.Collections.ObjectModel;
﻿using System.Data;
﻿using Pantry.Entity;
using Pantry.DataAccess;
using Pantry.Commons;

namespace Pantry.Business
{
    public sealed class ContactBus : IDisposable
    {
        private readonly ContactDao _contactDao;
        public ContactBus()
        {
            _contactDao = new ContactDao();
        }

        /// <summary>
        /// Gets the list contact.
        /// </summary>
        /// <returns></returns>
        public DataTable ListContact()
        {
            try
            {
                return _contactDao.ListContact();
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        /// <summary>
        /// Gets the list contact object.
        /// </summary>
        /// <returns></returns>
        public Collection<Contact> ListContactObject()
        {
            try
            {
                Collection<Contact> contacts = _contactDao.ListContactObject();
                return contacts;
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        /// <summary>
        /// Gets the list active contact.
        /// </summary>
        /// <returns></returns>
        public DataTable ListActivatedContact()
        {
            try
            {
                //_contactDao = new ContactDao();
                return _contactDao.ListActiveContact();
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }
        /// <summary>
        /// Gets the list active contact object.
        /// </summary>
        /// <returns></returns>
        public Collection<Contact> ListActiveContactObject()
        {
            try
            {
                return _contactDao.ListActiveContactObject();
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        /// <summary>
        /// Gets the contact object by id.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        /// <returns></returns>
        public Contact ContactObjectById(int contactId)
        {
            try
            {
                return _contactDao.ContactObjectById(contactId);
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }
        /// <summary>
        /// Creates the new contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public bool CreateNewContact(Contact contact)
        {
            try
            {
                var result = _contactDao.CreateNewContact(contact);
                return (result != 0);
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        /// <summary>
        /// Updates the contact.
        /// </summary>
        /// <param name="contact">The contact.</param>
        /// <returns></returns>
        public bool UpdateContact(Contact contact)
        {
            try
            {
                var result = _contactDao.UpdateContact(contact);
                return (result != 0);
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }
        /// <summary>
        /// Gets the manager name by id.
        /// </summary>
        /// <param name="managerId">The manager id.</param>
        /// <returns></returns>
        public string ManagerNameById(int managerId)
        {
            try
            {
                return _contactDao.ManagerNameById(managerId);
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }

        /// <summary>
        /// Checks the user is exists.
        /// </summary>
        /// <param name="mobilePhone">The mobile phone.</param>
        /// <param name="email">The email.</param>
        /// <returns></returns>
        public bool CheckUserIsExists(string mobilePhone, string email)
        {
            try
            {
                int result = _contactDao.CheckUserIsExists(mobilePhone, email);
                return (result != 0);
            }
            catch (Exception exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                throw;
            }
        }
        public DataTable ListContactType()
        {
            try
            {
                return _contactDao.ListContactType();
            }
            catch (Exception)
            {
                
                throw;
            }
        }
        public DataTable ListBestContactMethod()
        {
            try
            {
                return _contactDao.ListBestContactMethod();
            }
            catch (Exception)
            {
                
                throw;
            }
        }

        private void Dispose(bool disposing)
        {
            if (disposing)
            {
                _contactDao.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}